home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / bsrc_p1.arc / B_SCRIPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-30  |  12.7 KB  |  424 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  7. /*        \ 1011 /                                                          */
  8. /*         ------            KopyRong (K) 1987.  ALL RIGHTS REVERSED.       */
  9. /*                                                                          */
  10. /*                                                                          */
  11. /*               This module was written by Vince Perriello                 */
  12. /*                                                                          */
  13. /*                                                                          */
  14. /*                    BinkleyTerm Script Handler Module                     */
  15. /*                                                                          */
  16. /*                                                                          */
  17. /*   This  software  package is being distributed WITH FULL SOURCE CODE     */
  18. /*   with the  following  conditions:    1)  If  anything awful happens     */
  19. /*   because  you  use    it   (or  don't  use  it),  you  accept  full     */
  20. /*   responsibility;  2) you  don't start making tons of voice calls to     */
  21. /*   the authors to complain or  make  suggestions  about enhancements,     */
  22. /*   useful or otherwise;  3) you  do not reuse this code in commercial     */
  23. /*   products without specific permission to do so  from  the  authors;     */
  24. /*   4) If you find any problems you send  fixes  to  the  authors  for     */
  25. /*   inclusion  in  updates;    5) You find some way  to  express  your     */
  26. /*   appreciation  for  this  method of distribution, either by writing     */
  27. /*   code or  application  notes,  or  just sending along a "Thank You"     */
  28. /*   message.                                                               */
  29. /*                                                                          */
  30. /*   There is  copyrighted  code  in  this product.  We either wrote it     */
  31. /*   ourselves or got  permission  to use it.  Please don't force us to     */
  32. /*   pay a lawyer --  have some respect for our motives and don't abuse     */
  33. /*   this "license".                                                        */
  34. /*                                                                          */
  35. /*                                                                          */
  36. /*--------------------------------------------------------------------------*/
  37.  
  38. #include <stdio.h>
  39. #include <signal.h>
  40. #include <ctype.h>
  41. #include <conio.h>
  42. #include "com.h"
  43. #include "xfer.h"
  44.  
  45. extern int un_attended;
  46. extern int fullscreen;
  47.  
  48. /*--------------------------------------------------------------------------*/
  49. /*   Define our current script functions for use in our dispatch table.     */
  50. /*--------------------------------------------------------------------------*/
  51.  
  52. int    script_baud();           /* Set our baud rate to that of remote */
  53. int    script_xmit();           /* transmit characters out the port    */
  54. int    script_pattern();       /* define a pattern to wait for        */
  55. int    script_wait();           /* wait for a pattern or timeout       */
  56. int    script_dial();           /* dial the whole number at once       */
  57. int    script_areacode();    /* transmit the areacode out the port  */
  58. int    script_phone();        /* transmit the phone number           */
  59. int    script_carrier();       /* Test point, must have carrier now   */
  60. int     script_session();       /* Exit script "successfully"          */
  61. int     script_if();          /* Branch based on pattern match       */
  62. int     script_goto();          /* Absolute branch                     */
  63.  
  64. struct dispatch {
  65.     char *string;
  66.     int (*fun)();
  67.     };
  68.  
  69. struct dispatch disp_table[] = {
  70.     {    "baud",        script_baud    },
  71.     {    "xmit",        script_xmit    },
  72.     {    "pattern",    script_pattern    },
  73.     {    "wait",        script_wait    },
  74.     {    "dial",        script_dial    },
  75.     {    "areacode",    script_areacode },
  76.     {    "phone",    script_phone },
  77.     {    "carrier",    script_carrier    },
  78.     {    "session",    script_session    },
  79.    {  "if",       script_if },
  80.    {  "goto",     script_goto },
  81.     {    NULL,        NULL        }
  82.     };
  83.  
  84. char *script_dial_string = NULL;    /* string for 'dial'     */
  85. char *script_phone_string = NULL;    /* string for 'phone'    */
  86. char *script_areacode_string = "   ";    /* string for 'areacode' */
  87.  
  88. #define PATTERNS 9
  89. #define PATSIZE 22
  90. char pattern[PATTERNS][PATSIZE];            /* 'wait' patterns       */
  91. int scr_session_flag = 0;        /* set by "session".     */
  92. int pat_matched = -1;
  93. char *script_function_argument;        /* argument for functions*/
  94.  
  95. #define MAX_LABELS 50
  96. #define MAX_LAB_LEN 20
  97. struct lab {
  98.    char name[MAX_LAB_LEN + 1];
  99.    long foffset;
  100.    int line;
  101. } labels[MAX_LABELS];
  102.  
  103. static long offset;
  104. static int num_labels = 0;
  105. static int curline;
  106. static FILE *stream;
  107.  
  108. extern char *BINKpath;            /* path to Bink variables*/
  109. extern int autobaud;
  110.  
  111. static char temp[256];
  112.  
  113. do_script(phone_number)
  114. char *phone_number;
  115. {
  116. register int i,j,k;
  117. register char *c,*f;
  118. char s[64], *t;
  119.  
  120. /*--------------------------------------------------------------------------*/
  121. /* Reset everything from possible previous use of this function.            */
  122. /*--------------------------------------------------------------------------*/
  123.  
  124. curline = 0;
  125. pat_matched = -1;
  126. num_labels = 0;
  127. *script_areacode_string = '\0';            /* reset the special strings */
  128. script_dial_string = script_phone_string = NULL;
  129. for (i = 0; i < PATTERNS; i++)
  130.     {
  131.     pattern[i][0] = 1;
  132.     pattern[i][1] = '\0';            /* and the 'wait' patterns   */
  133.     }
  134. scr_session_flag = 0;
  135.  
  136. /*--------------------------------------------------------------------------*/
  137. /* Now start doing things with phone number:                                */
  138. /*     1) construct the name of the script file into temp                   */
  139. /*     2) build script_dial_string, script_areacode_string and              */
  140. /*        script_phone_string                                               */
  141. /*--------------------------------------------------------------------------*/
  142.  
  143. strcpy(temp,BINKpath);                /* get our current path      */
  144. t = c = &temp[strlen(temp)];            /* point past paths          */
  145. f = phone_number;                /* then get input side       */
  146. while (*++f != '\"')                /* look for end of string    */
  147.     {
  148.     if ((*c++ = *f) == '\0')        /* if premature ending,      */
  149.         return(0);
  150.     }
  151. *c = '\0';                    /* Now we have the file name */
  152. strcpy (s, t);
  153.  
  154. script_dial_string = ++f;            /* dial string is rest of it */
  155.  
  156. /* Say what we are doing */
  157. status_line (":Dialing %s with script \"%s\"", script_dial_string, s);
  158.  
  159. c = script_areacode_string;            /* point to area code        */
  160. for (i = 0; i < 3, *f != '\0', *f != '-'; i++)
  161.     *c++ = *f++;                /* copy it for 'areacode'    */
  162. *c = '\0';                    /* terminate areacode        */
  163. if (*f++ == '-')
  164.     script_phone_string = f;        /* point to phone string     */
  165.  
  166. /*--------------------------------------------------------------------------*/
  167. /* Finally open the script file and start doing some WORK.                  */
  168. /*--------------------------------------------------------------------------*/
  169.  
  170. if ((stream = fopen(temp,"r")) == NULL)        /* OK, let's open the file   */
  171.    {
  172.    status_line ("!Could not open script %s", temp);
  173.     return(0);                /* no file, no work to do    */
  174.    }   
  175.  
  176. k = 0;                        /* default return is "fail"  */
  177. while(nextline(NULL))        /* Now we parse the file ... */
  178.     {
  179.     k = 0;                    /* default return is "fail"  */
  180.     for (j = 0; (c = disp_table[j].string) != NULL; j++)
  181.         {
  182.         i = strlen(c);
  183.         if (strnicmp(temp,c,i) == 0)
  184.             {
  185.             script_function_argument = temp + i + 1;
  186.          if (un_attended && fullscreen)
  187.             {
  188.             gotoxy (0,18);
  189.             }
  190.             k = (*disp_table[j].fun)();
  191.             break;
  192.             }
  193.         }
  194.     if (!k || scr_session_flag)        /* get out for failure or    */
  195.         break;                /* 'session'.                */
  196.  
  197.     }
  198. fclose(stream);                    /* close input file          */
  199. if (!k)
  200.    status_line ("+Script \"%s\" failed at line %d", s, curline);
  201. return(k);                    /* return last success/fail  */
  202. }
  203.  
  204. script_xmit()
  205. {
  206. mdm_cmd_string(script_function_argument,1);
  207. return(1);
  208. }
  209.  
  210. script_areacode()
  211. {
  212. mdm_cmd_string(script_areacode_string,0);
  213. return(1);
  214. }
  215.  
  216. script_phone()
  217. {
  218. mdm_cmd_string(script_phone_string,0);
  219. return(1);
  220. }
  221.  
  222. script_dial()
  223. {
  224. long t,timerset();
  225. mdm_cmd_string(script_dial_string,0);
  226. mdm_cmd_char(CR);                /* terminate the string      */
  227. if (modem_response(7500))            /* we got a good response,   */
  228.     {
  229.     timer(20);                /* wait for other side       */
  230.     return(1);                /* Carrier should be on now  */
  231.     }
  232. return(0);    /* no good */
  233. }
  234.  
  235. script_carrier()
  236. {
  237. return(CARRIER);
  238. }
  239.  
  240. script_session()
  241. {
  242. ++scr_session_flag;                /* signal end of script */
  243. return(1);
  244. }
  245.  
  246. script_pattern()
  247. {
  248. register int i,j;
  249. register char *c;
  250.  
  251. c = script_function_argument;            /* copy the pointer   */
  252. i = atoi(c);                    /* get pattern number */
  253. if (i < 0 || i >= PATTERNS)                /* check bounds */
  254.     return(0);
  255. c += 2;                        /* skip digit and space */
  256. for (j = 1; j <= PATSIZE, *c != '\0'; j++)
  257.     pattern[i][j] = *c++;            /* store the pattern */
  258. pattern[i][j] = '\0';                /* terminate it here */
  259. return(1);
  260. }
  261.  
  262. script_wait()
  263. {
  264. long t1,timerset();
  265. register int i,j;
  266. register char c;
  267. int wait;
  268. int got_it = 0;
  269.  
  270. pat_matched = -1;
  271. wait = 100 * atoi(script_function_argument);    /* try to get wait length */
  272. if (!wait)
  273.     wait = 4000;                /* default is 40 seconds  */
  274. t1 = timerset(wait);
  275. cprintf ("\r\n\033[K");
  276. while (!timeup(t1) && !KEYPRESS())
  277.     {
  278.     if (!CHAR_AVAIL())            /* if nothing ready yet,     */
  279.         {
  280.         time_release();            /* give others a shot        */
  281.         continue;            /* just process timeouts     */
  282.         }
  283.     t1 = timerset(wait);            /* reset the timeout         */
  284.     c = MODEM_IN();                /* get a character           */
  285.     if (!c)    continue;            /* ignore null characters    */
  286.    if (c >= ' ')
  287.       {
  288.         WRITE_ANSI(c&0x7f);
  289.       }
  290.     for (i = 0; i < PATTERNS; i++)
  291.         {
  292.         j = pattern[i][0];        /* points to next match char */
  293.         if (c == pattern[i][j])        /* if it matches,            */
  294.             {
  295.             ++j;            /* bump the pointer          */
  296.             pattern[i][0] = j;    /* store it                  */
  297.             if (!pattern[i][j])    /* if at the end of pattern, */
  298.                 {
  299.                 ++got_it;
  300.             pat_matched = i;
  301.                 goto done;
  302.                 }
  303.             }
  304.         else
  305.             {
  306.             pattern[i][0] = 1;    /* back to start of string   */
  307.             }
  308.         }
  309.     }
  310. done:
  311. for (i = 0; i < PATTERNS; i++)
  312.     {
  313.     pattern[i][0] = 1;            /* reset these for next time */
  314.     }
  315. return(got_it);
  316. }
  317.  
  318. script_baud()
  319. {
  320. int a, b;
  321.  
  322. if ((b = atoi (script_function_argument)) != 0)
  323.     {
  324.     a = autobaud;
  325.     autobaud = 0;
  326.     set_baud (b, 0);
  327.     autobaud = a;
  328.     }
  329. return(1);
  330. }
  331.  
  332. script_goto ()
  333. {
  334.    int i;
  335.  
  336.    /* First see if we already found this guy */
  337.    for (i = 0; i < num_labels; i++)
  338.       {
  339.       if (stricmp (script_function_argument, labels[i].name) == 0)
  340.          {
  341.          /* We found it */
  342.          fseek (stream, labels[i].foffset, SEEK_SET);
  343.          curline = labels[i].line;
  344.          return (1);
  345.          }
  346.       }
  347.  
  348.    return (nextline (script_function_argument));
  349. }
  350.  
  351. script_if ()
  352. {
  353.    /* Can we go away real quick? */
  354.    if (atoi (script_function_argument) != pat_matched)
  355.       return (1);
  356.  
  357.    /* Skip the pattern number and the space */
  358.    script_function_argument += 2;
  359.  
  360.    return (script_goto ());
  361. }
  362.  
  363. nextline (str)
  364. char *str;
  365. {
  366.    char save[256];
  367.  
  368.    if (str != NULL)
  369.       strcpy (save, str);
  370.    else
  371.       save[0] = '\0';
  372.  
  373. while(get_line ())        /* Now we parse the file ... */
  374.     {
  375.    if (!isalpha(temp[0]))
  376.       {
  377.       if (temp[0] != ':')
  378.          {
  379.          /* This line is a comment line */
  380.          continue;
  381.          }
  382.       else
  383.          {
  384.          /* It is a label */
  385.          if (num_labels >= MAX_LABELS)
  386.             {
  387.             status_line ("!Too many labels in script");
  388.             return (0);
  389.             }
  390.          strcpy (labels[num_labels].name, &(temp[1]));
  391.          labels[num_labels].foffset = offset;
  392.          labels[num_labels].line = curline;
  393.          ++num_labels;
  394.  
  395.          if (stricmp (&temp[1], save))
  396.             {
  397.             continue;
  398.             }
  399.          else
  400.             {
  401.             return (1);
  402.             }
  403.          }
  404.       }
  405.  
  406.    if (!save[0])
  407.       return (1);
  408.    }
  409.  
  410.    return (0);
  411. }
  412.  
  413. get_line ()
  414. {
  415.    if (fgets (temp, 255, stream) == NULL)
  416.       return (0);
  417.  
  418.    ++curline;
  419.     temp[strlen(temp)-1] = '\0';
  420.    cprintf ("\r\nScript Line %03d: %-62.62s", curline, temp);
  421.    offset = ftell (stream);
  422.    return (1);
  423. }
  424.